﻿/*

  The contents of this file are subject to the Mozilla Public License Version
  1.1 (the "License"); you may not use this file except in compliance with
  the License. You may obtain a copy of the License at 
  
           http://www.mozilla.org/MPL/ 
  
  Software distributed under the License is distributed on an "AS IS" basis,
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  for the specific language governing rights and limitations under the License. 
  
  The Original Code is KazeOS Framework.
  
  The Initial Developer of the Original Code is
  ALCARAZ Marc (aka eKameleon) <ekameleon@gmail.com>.
  Portions created by the Initial Developer are Copyright (C) 2004-2010
  the Initial Developer. All Rights Reserved.
  
  Contributor(s) :
  
*/

package evisio 
{
    import core.reflect.getClassPath;

    import evisio.controllers.models.guest.ChangeGuest;
    import evisio.controllers.models.guest.ClearGuest;
    import evisio.controllers.models.user.ChangeUser;
    import evisio.controllers.models.user.ClearUser;
    import evisio.controllers.net.Disconnect;
    import evisio.controllers.net.NetStatus;
    import evisio.display.application.Body;
    import evisio.display.application.Footer;
    import evisio.display.application.Header;
    import evisio.display.application.Wallpaper;
    import evisio.formatters.UserFormatter;
    import evisio.managers.ScrollStateManager;
    import evisio.managers.StateManager;
    import evisio.net.NetServerConnectionListener;
    import evisio.net.PeerIDExpert;
    import evisio.net.StratusConnectionListener;
    import evisio.net.StreamInExpert;
    import evisio.net.StreamOutExpert;
    import evisio.process.application.InitApplication;
    import evisio.states.ask.AskState;
    import evisio.states.authentification.AuthentificationState;
    import evisio.states.chat.ChatState;
    import evisio.vo.UserVO;

    import graphics.FillGradientStyle;
    import graphics.FillStyle;
    import graphics.LineStyle;

    import lunas.components.buttons.LightButton;
    import lunas.components.buttons.LightFrameLabelButton;
    import lunas.components.textinputs.VisualTextInput;

    import system.evaluators.EdenEvaluator;
    import system.events.ActionEvent;
    import system.logging.LoggerLevel;
    import system.logging.targets.SOSTarget;

    import vegas.display.ApplicationDisplay;
    import vegas.display.Protector;
    import vegas.ioc.net.ECMAObjectLoader;
    import vegas.managers.ProtectorManager;
    import vegas.managers.TabManager;
    import vegas.media.CameraExpert;
    import vegas.media.MicrophoneExpert;
    import vegas.models.CoreModelObject;
    import vegas.net.NetServerConnection;
    import vegas.net.NetStreamExpert;

    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.text.TextFormat;

    [SWF(width="960", height="780", frameRate="24", backgroundColor="#666666")]
    
    /**
     * Main application of the Stratus test.
     * <p><b>Note :</b> this application must be compiled with the FP10 target with the FLEX SDK 3.2 or FlashCS4.</p>
     * <p>This application is a beta test of the Adobe Stratus application and use it to connect two FP10 SWF clients.</p>
     * <p>To connect to the Stratus service see the <a href="http://labs.adobe.com/technologies/stratus/">Adobe labs project page of Stratus</a>.</p>
     * <p>Additional developer documentation may be found in the <a href="http://help.adobe.com/en_US/AS3LCR/Flash_10.0/index.html">Flash Player 10 API documentation</a> for the <b>NetConnection</b> and <b>NetStream</b> ActionScript classes, the <a href="http://labs.adobe.com/wiki/index.php/Stratus:FAQ">Stratus FAQ</a>, and the <a href="http://www.adobe.com/devnet/flashplayer/articles/rtmfp_stratus_app.html">Developing End-to-End Applications using Flash Player 10 and Stratus Service article in the Adobe Developer Center</a>.</p>  
     */
    public class Application extends ApplicationDisplay 
    {
        /**
         * Creates a new Application instance.
         */
        public function Application()
        {
            // stage
            
            stage.align                  = StageAlign.TOP_LEFT ; 
            stage.scaleMode              = StageScaleMode.NO_SCALE ;
            stage.showDefaultContextMenu = false ;
            
            // logging
            
            var sosTarget:SOSTarget = new SOSTarget( getClassPath(this) , 0xFFD9A2 ) ;
            sosTarget.filters       = ["*"] ;
            sosTarget.level         = LoggerLevel.ALL ;
            sosTarget.includeLines  = true ;
            sosTarget.includeTime   = true ;
            
            // IoC
              
            var contextFile:String = flashVars.getValue("contextFile") || "application.eden"   ;
            var contextPath:String = flashVars.getValue("contextPath") || "context/" ;
            
            var loader:ECMAObjectLoader = new ECMAObjectLoader( contextFile , contextPath ) ;
            
            // loader.verbose = true ;
            
            loader.addEventListener( ActionEvent.START  , start ) ;
            loader.addEventListener( ActionEvent.FINISH , finish ) ;
            
            // loader.verbose = true ;
            
            loader.root = this ;
            
            loader.run() ;
        }
        
        /**
         * The wait field.
         */
        protected var field:TextField ;
        
        /**
         * Invoked when the IoC factory is initialized.
         */
        protected function finish(e:Event):void
        {
            logger.info( this + " :: " + e.type ) ; 
            removeEventListener( Event.ADDED, _onAdded ) ;
            removeChild(field) ;
            field = undefined ;
        }
        
        /**
         * Invoked when the IoC factory start the initialization.
         */
        protected function start(e:Event):void
        {
            logger.info( this + " :: " + e.type ) ;
            
            addEventListener(Event.ADDED, _onAdded) ;
            
            var progressColor:Number   = ( flashVars.getValue("progressColor", new EdenEvaluator()) || 0xFFFFFF ) as Number  ;
            var progressMessage:String = flashVars.getValue("progressMessage") || "Loading in progress..."; 
            
            field                   = new TextField() ;
            field.autoSize          = TextFieldAutoSize.LEFT ;
            field.defaultTextFormat = new TextFormat( "Verdana", 12, progressColor, true ) ;
            field.selectable        = false ;
            field.x                 = 10 ;
            field.y                 = 10 ;
            field.text              = progressMessage ;
            
            addChild(field) ;
        }
        
        /**
         * @private
         */
        private function _onAdded( e:Event ):void
        {
            if ( field != null )
            {
                setChildIndex(field, numChildren-1) ;
            }
        }
        
        // vegas
        
        CoreModelObject ; 
        Protector ;
        ProtectorManager ; TabManager ;
        CameraExpert ; MicrophoneExpert ;
        NetServerConnection ; NetStreamExpert ;
        
        // lunas
        
        LightButton ; LightFrameLabelButton ;
        VisualTextInput ;
        
        // graphics
        
        LineStyle ; FillGradientStyle ; FillStyle ;
        
        ////////// evisio
        
        ChangeGuest ; ClearGuest ;
        ChangeUser ; ClearUser ;
        Disconnect ; NetStatus ; 
        Body ; Footer ; Header ; Wallpaper ; 
        UserFormatter ; 
        ScrollStateManager ; StateManager ;
        NetServerConnectionListener ; PeerIDExpert ; 
        StratusConnectionListener ; StreamInExpert ; StreamOutExpert ;
        
        // evisio.process.application
        
        InitApplication ;
        
        // evisio.states
        
        AskState ; AuthentificationState ; ChatState ;
        
        /////////////// register value objects
        
        UserVO.register() ;
    }
}
